package cn.myzqu.sign.project.server.service.impl;


import cn.myzqu.sign.project.server.base.exception.CustomException;
import cn.myzqu.sign.project.server.dao.ProjectStaffMapper;
import cn.myzqu.sign.project.server.entity.ProjectStaff;
import cn.myzqu.sign.project.server.service.ProjectStaffService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 的川
 * @Date: 2019/3/5 17:18
 */
@Service
@Slf4j
public class ProjectStaffServiceImpl implements ProjectStaffService {

    @Autowired
    private ProjectStaffMapper projectStaffMapper;

    @Override
    public Boolean add(ProjectStaff projectStaff){
        //查看该用户是否已经报名
        if(this.projectHasUser(projectStaff.getProjectInfoId(),projectStaff.getUserId())){
            throw new CustomException(1,"您已经提交过报名！");
        }
        projectStaff.setIsSign((byte)1);
        projectStaff.setStatus((byte)2);
        if(projectStaffMapper.insert(projectStaff)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<ProjectStaff> getNeedSignList(String projectId) {
        QueryWrapper<ProjectStaff> queryWrapper = new QueryWrapper();
        //已通过审核的需要签到的
        queryWrapper.eq("project_info_id",projectId)
                .eq("is_sign",1)
                .eq("status",1);
        return projectStaffMapper.selectList(queryWrapper);
    }

    @Override
    public ProjectStaff getByUserAndProject(String userId, String projectId) {
        QueryWrapper<ProjectStaff> queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_info_id",projectId)
                .eq("user_id",userId);
        return projectStaffMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean projectHasUser(String projectId, String userId) {
        QueryWrapper<ProjectStaff> queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_info_id",projectId)
                .eq("user_id",userId);
        if(projectStaffMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean projectHasUser(String projectId, String userId, Byte status , Byte isSign) {
        QueryWrapper<ProjectStaff> queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_info_id",projectId)
                .eq("user_id",userId);
        if(status!=null){
            queryWrapper.eq("status",status);
        }
        if(isSign!=null){
            queryWrapper.eq("is_sign",isSign);
        }
        if(projectStaffMapper.selectCount(queryWrapper)>0){
            return true;
        }
        return false;
    }

//    @Override
//    public ProjectStaff getByUserAndProject(String userId , String projectId){
//        QueryWrapper<ProjectStaff> queryWrapper = new QueryWrapper();
//        //已通过审核的需要签到的
//        queryWrapper.eq("project_info_id",projectId)
//                .eq("user_id",userId);
//        return projectStaffMapper.selectOne(queryWrapper);
//    }


    @Override
    public List<Map> search(Map params){
        //查看该projectId是否存在
        //查看该用户是否已经报名
        return projectStaffMapper.search(params);
    }

}
